﻿using System;
using System.Windows;
using System.Windows.Media;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using Microsoft.Research.DynamicDataDisplay.PointMarkers;

namespace D3StockChart.Tester
{
    public partial class MainWindow
    {
        private readonly PriceFeeder _priceFeeder;
        private StockInfo _lastStockInfo;

        private ObservableDataSource<StockInfo> _lastPrice;
        private LineGraph _chartLastPrice;

        private ObservableDataSource<StockInfo> _orderBTO;
        private LineAndMarker<ElementMarkerPointsGraph> _chartOrderBTO;

        public MainWindow()
        {
            InitializeComponent();
            _priceFeeder = new PriceFeeder(100m);
            initSource();
        }

        private void initSource()
        {
            plotter.Background = Brushes.Black;
            _lastPrice = initLine(ref _chartLastPrice, Colors.Cyan, 2, "Last Price");
            _orderBTO = initMarker(ref _chartOrderBTO, Brushes.Green, "Long Open");
            plotter.LegendVisible = false;
        }

        private ObservableDataSource<StockInfo> initLine(ref LineGraph chart, Color color, double lineThickness, string name)
        {
            ObservableDataSource<StockInfo> ods = new ObservableDataSource<StockInfo>();
            ods.SetXMapping(ci => dateAxis.ConvertToDouble(ci.Date));
            ods.SetYMapping(ci => ci.Rate);
            chart = plotter.AddLineGraph(ods, color, lineThickness, name);
            return ods;
        }

        private ObservableDataSource<StockInfo> initMarker(ref LineAndMarker<ElementMarkerPointsGraph> chart, Brush markcolor, string description)
        {
            ObservableDataSource<StockInfo> ods = new ObservableDataSource<StockInfo>();
            ods.SetXMapping(ci => dateAxis.ConvertToDouble(ci.Date));
            ods.SetYMapping(ci => ci.Rate);
            ods.AddMapping(ShapeElementPointMarker.ToolTipTextProperty,
                           ci => String.Format("{0} {1} on {2:hh:MM:ss}", description, ci.Rate, ci.Date));
            chart = plotter.AddLineGraph(ods,
                null, //new Pen(Brushes.DarkGoldenrod, 3),
                new CircleElementPointMarker { Size = 10, Brush = Brushes.Yellow, Fill = markcolor },
                new PenDescription(description));
            return ods;
        }
        private void clear(ObservableDataSource<StockInfo> ods)
        {
            if (ods == null)
                return;
            ods.Collection.Clear();
        }

        private void resetButton_Clicked(object sender, RoutedEventArgs e)
        {
            clear(_lastPrice);
            clear(_orderBTO);
        }

        private void newPriceButton_Clicked(object sender, RoutedEventArgs e)
        {
            double newPrice = (double) _priceFeeder.NextPrice();
            _lastStockInfo = new StockInfo { Date = DateTime.Now, Rate = newPrice };
            _lastPrice.AppendAsync(Dispatcher, _lastStockInfo);
        }

        private void newMarkButton_Clicked(object sender, RoutedEventArgs e)
        {
            _orderBTO.AppendAsync(Dispatcher, _lastStockInfo);
        }
    }
}
